package com.jeffgalper.feather;

import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

public class FeatherOutputStream extends FilterOutputStream {

   private OutputStream mOut;

   public FeatherOutputStream(OutputStream out) {
      super(out);
      mOut = out;
   }

   public void putNextFeatherable(Featherable featherable) throws IOException, FeatherException {
      DataOutput output = new DataOutputStream(mOut);
      try {
         output.write(Feather.MAGIC_FEATHER_STRING.getBytes("US-ASCII"));
      } catch (UnsupportedEncodingException e) {
         throw new RuntimeException("Could not encode magic number correctly",
               e);
      }
      output.writeShort(Feather.VERSION);
      output.writeShort(featherable.getEPathSize());
      output.write(featherable.getEPathToArray());
      output.writeByte(featherable.getBitFieldByte());
   }

}
